//Per-vertex Phong lighting
uniform bool LocalViewer;

void main(void)
{
	vec4 colour = vec4(0.0);
	vec4 vert = gl_Vertex;

	//normalized vertex normal
	vec3 normal = normalize(vec3(gl_NormalMatrix * gl_Normal));
	
	//unit vector in direction of light (already has modelview matrix applied)
	vec3 light = normalize(vec3(gl_LightSource[0].position));
   
   //reflection vector
   vec3 reflection = normalize( reflect( -light, normal ) );

   //calculating view vector
   vec3 eyeVec = vec3 (gl_ModelViewMatrix * gl_Vertex);
   vec3 viewVec = normalize(-eyeVec);

	//compute diffuse scalar
	float NdotL = max(dot(normal, light), 0.0);
	
	//add specular component, if needed
	if( NdotL > 0.0 )
	{
	   float RdotV = max( dot( reflection, viewVec ), 0.0 ); 
	   colour += gl_FrontMaterial.specular * gl_LightSource[0].specular 
	           * pow( RdotV, gl_FrontMaterial.shininess);
	}
	
	//add global and light ambient
	colour += gl_FrontMaterial.ambient * (gl_LightModel.ambient + gl_LightSource[0].ambient);

	//add diffuse component
	colour += NdotL * gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
	
	//set the colour
	gl_FrontColor = colour;
	
	//apply matrix transforms to vertex position
	gl_Position = gl_ModelViewProjectionMatrix * vert;
}
